/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.vus;

import android.app.Activity;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.bankscene.screenfit.R;
import com.bankscene.screenfit.events.EventBusEvents;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.bankscene.screenfit.events.EventBusEvents.Go2MainActivity.Go2MainActivityFlag.GOHONE;

/**
 * Created by jiiiiiin on 6/14/16.
 */
public class MenusVu extends BaseVu {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @Nullable
    @BindView(R.id.nav_view)
    NavigationView navView;

    @Override
    public void init(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable EventBus bus) {
        this.bus = bus;
        rootView = inflater.inflate(R.layout.menusactivity_layout, container, false);
        unbinder = ButterKnife.bind(this, rootView);
//        drawerLayout.setStatusBarBackground(colorPrimaryDark);
        if (navView != null) {
            setupDrawerContent(navView);
        }

    }

    protected void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        bus.post(new EventBusEvents.OnNavigationItemSelectedEvent(menuItem));
                        // Close the navigation drawer when an item is selected.
                        menuItem.setChecked(true);
                        drawerLayout.closeDrawers();
                        return true;
                    }
                });
    }

    @OnClick(value = R.id.fab_gohome_task)
    void OnClick() {
        bus.post(new EventBusEvents.Go2MainActivity(GOHONE));
    }

    public void setToolbar(@NonNull Activity activity,@NonNull ActionBar ab,@NonNull boolean isPad) {
        ab.setDisplayHomeAsUpEnabled(true);
        if (!isPad) {
            ab.setHomeAsUpIndicator(R.drawable.ic_menu);
//            vu.getToolbar().setNavigationIcon(R.drawable.ic_menu);//setNavigationIcon需要放在 setSupportActionBar之后才会生效
            ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(activity, drawerLayout, toolbar, R.string.menus_navigation_open, R.string.menus_navigation_close);
            drawerToggle.syncState();
            drawerLayout.addDrawerListener(drawerToggle);
        }
    }

    @CheckResult
    public Toolbar getToolbar() {
        return toolbar;
    }

    @CheckResult
    public DrawerLayout getDrawerLayout() {
        return drawerLayout;
    }
}
